package com.hyj.boxueguxiangmu.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hyj.boxueguxiangmu.R;
import com.hyj.boxueguxiangmu.activity.DetailsActivity;
import com.hyj.boxueguxiangmu.model.ListNews;


import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by lucashu on 2018/4/1.
 */

public class ExerciseFragment extends Fragment {
    private static Gson gson;
    ListView listView;
    ArrayList<ListNews> lists = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exercise, container, false);
        listView = view.findViewById(R.id.mlistview);
        loaingdata();
        return view;
    }


    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ListNews listNew = lists.get(position);
            ViewHolder holder;
            View view;
            if (convertView == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(getActivity()).inflate(R.layout.item_news, null);
                holder.contentText = (TextView) view.findViewById(R.id.content_text);
                holder.timeText = (TextView) view.findViewById(R.id.time_text);
                holder.clickview = (RelativeLayout) view.findViewById(R.id.clickview);

                view.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            holder.contentText.setText(listNew.contentTitle);
            holder.timeText.setText(listNew.submitTime);
            holder.clickview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), DetailsActivity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putString("contentid", listNew.contentID);
                    intent.putExtras(mBundle);
                    startActivity(intent);
                }
            });
            return view;
        }
    }

    static class ViewHolder {
        private TextView contentText;
        private TextView timeText;
        private RelativeLayout clickview;
    }

    public void loaingdata() {
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "7");
        params.addQueryStringParameter("v1", "274");
        params.addQueryStringParameter("v2", "1");
        params.addQueryStringParameter("v3", "50");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.i("lucashu", "result:" + result);
                String result111 = convertString(result);
                Type listType = new TypeToken<ArrayList<ListNews>>() {
                }.getType();
                if (gson == null) {
                    gson = new Gson();
                }

                lists = gson.fromJson(result111, listType);
                listView.setAdapter(new MyAdapter());
//                NewList data = gson.fromJson(result111, NewList.class);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");

        return result1;

    }

}
